using MicroMsgBackground.Common.Utils;
using MicroMsgBackground.Storage;
using System;
using System.IO.IsolatedStorage;
namespace MicroMsgBackground.Manager
{
	public class RecycleMgr
	{
		private const string TAG = "RecycleMgr";
		private const int FILE_RECYLE_INTERVAL_TIME = 10080;
		private const long SNS_FILE_RECYCLE_INTERVAL = 432000000L;
		private const int SNS_FOLDER_RESERVE_FILE_NUM = 15;
		public const long MAX_LAUNCH_TIME = 20000L;
		private const string recycleInfoXmlfileName = "background/recycle.xml";
		private static RecycleInfo sRecyleInfo;
		private static long currentTicks = 0L;
		public static bool needRecyle()
		{
			if (RecycleMgr.sRecyleInfo.lastRecyleStartTime == 0L)
			{
				RecycleMgr.sRecyleInfo.lastRecyleStartTime = Util.getNowMilliseconds();
				RecycleMgr.sRecyleInfo.recyleFileIndex = -1;
				RecycleMgr.save(RecycleMgr.sRecyleInfo);
				return false;
			}
			if (RecycleMgr.sRecyleInfo.recyleFileIndex >= 0 && RecycleMgr.sRecyleInfo.recyleFileIndex <= 99)
			{
				return true;
			}
			if (RecycleMgr.sRecyleInfo.recyleFileIndex == -1 && Util.getNowMilliseconds() - RecycleMgr.sRecyleInfo.lastRecyleStartTime >= 432000000L)
			{
				RecycleMgr.sRecyleInfo.recyleFileIndex = 0;
				RecycleMgr.sRecyleInfo.lastRecyleStartTime = Util.getNowMilliseconds();
				RecycleMgr.save(RecycleMgr.sRecyleInfo);
				return true;
			}
			if (RecycleMgr.sRecyleInfo.recyleFileIndex >= -1 && RecycleMgr.sRecyleInfo.recyleFileIndex <= 99)
			{
				return false;
			}
			RecycleMgr.sRecyleInfo.recyleFileIndex = 0;
			RecycleMgr.sRecyleInfo.lastRecyleStartTime = Util.getNowMilliseconds();
			RecycleMgr.save(RecycleMgr.sRecyleInfo);
			return true;
		}
		public static void setCurrentTick()
		{
			RecycleMgr.currentTicks = Util.getCurrentTick();
		}
		public static bool isRecyleOverTime()
		{
			return Util.getTimeSpan(RecycleMgr.currentTicks) > 20000L;
		}
		public static bool isInRecycleTime()
		{
			DateTime now = DateTime.get_Now();
			return now.get_Hour() >= 2 && now.get_Hour() <= 4;
		}
		private static bool doRecyleInDirectory(string dirPath)
		{
			if (RecycleMgr.isRecyleOverTime())
			{
				Log.smethod_6("RecycleMgr", "recyle consume limit time dir:" + dirPath);
				return true;
			}
			if (string.IsNullOrEmpty(dirPath))
			{
				return false;
			}
			try
			{
				using (IsolatedStorageFile userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication())
				{
					bool result;
					if (!userStoreForApplication.DirectoryExists(dirPath))
					{
						result = false;
						return result;
					}
					string[] fileNames = userStoreForApplication.GetFileNames(dirPath + "/*");
					if (fileNames == null)
					{
						result = false;
						return result;
					}
					int num = fileNames.Length;
					if (num <= 15)
					{
						result = false;
						return result;
					}
					long[] array = new long[num];
					long[] array2 = new long[num];
					if (array != null && array.Length == num && array2 != null)
					{
						if (array2.Length == num)
						{
							int num2 = 0;
							string[] array3 = fileNames;
							for (int i = 0; i < array3.Length; i++)
							{
								string text = array3[i];
								string text2 = dirPath + "/" + text;
								long num3 = (long)Math.Abs((int)(DateTime.get_Now() - userStoreForApplication.GetLastAccessTime(text2)).get_TotalMinutes());
								array[num2] = num3;
								array2[num2] = num3;
								num2++;
							}
							Array.Sort<long>(array2);
							long num4 = array2[14];
							long num5 = (num4 > 10080L) ? num4 : 10080L;
							for (int j = 0; j < num; j++)
							{
								if (array[j] > num5)
								{
									string fileName = dirPath + "/" + fileNames[j];
									StorageIO.deleteFile(fileName);
								}
								if (j % 25 == 0 && RecycleMgr.isRecyleOverTime())
								{
									Log.smethod_6("RecycleMgr", "recyle consume limit time" + dirPath);
									result = true;
									return result;
								}
							}
							goto IL_197;
						}
					}
					result = false;
					return result;
				}
				IL_197:;
			}
			catch (Exception ex)
			{
				Log.smethod_1("RecycleMgr", string.Concat(new object[]
				{
					"strPath: ",
					dirPath,
					"\n",
					ex
				}));
				bool result = false;
				return result;
			}
			return false;
		}
		public static void doFileRecycle()
		{
			Log.smethod_5("RecycleMgr", string.Concat(new object[]
			{
				"start recyle startIndex:",
				RecycleMgr.sRecyleInfo.recyleFileIndex,
				" last start Time",
				RecycleMgr.sRecyleInfo.lastRecyleStartTime
			}));
			string text = "storage/" + AccountMgr.getCurAccount().strUsrName + "/sns";
			if (string.IsNullOrEmpty(text))
			{
				return;
			}
			try
			{
				IsolatedStorageFile userStoreForApplication;
				IsolatedStorageFile isolatedStorageFile = userStoreForApplication = IsolatedStorageFile.GetUserStoreForApplication();
				try
				{
					if (!isolatedStorageFile.DirectoryExists(text))
					{
						return;
					}
				}
				finally
				{
					if (userStoreForApplication != null)
					{
						userStoreForApplication.Dispose();
					}
				}
			}
			catch (Exception ex)
			{
				Log.smethod_1("RecycleMgr", string.Concat(new object[]
				{
					"strPath: ",
					text,
					"\n",
					ex
				}));
				return;
			}
			string text2 = text + "/down/";
			for (int i = RecycleMgr.sRecyleInfo.recyleFileIndex; i < 100; i++)
			{
				string dirPath = text2 + i;
				if (RecycleMgr.doRecyleInDirectory(dirPath))
				{
					Log.LogToFile();
					return;
				}
				RecycleMgr.sRecyleInfo.recyleFileIndex = i + 1;
				if (RecycleMgr.sRecyleInfo.recyleFileIndex == 100)
				{
					RecycleMgr.sRecyleInfo.recyleFileIndex = -1;
				}
				RecycleMgr.save(RecycleMgr.sRecyleInfo);
			}
		}
		public static bool save(RecycleInfo info)
		{
			return StorageXml.saveToFile<RecycleInfo>(info, "background/recycle.xml");
		}
		public static RecycleInfo load()
		{
			return StorageXml.loadFromFile<RecycleInfo>("background/recycle.xml");
		}
		public static void loadRecyleInfo()
		{
			RecycleMgr.sRecyleInfo = RecycleMgr.load();
			if (RecycleMgr.sRecyleInfo == null)
			{
				RecycleMgr.sRecyleInfo = new RecycleInfo();
				RecycleMgr.sRecyleInfo.recyleFileIndex = -1;
				RecycleMgr.sRecyleInfo.lastRecyleStartTime = 0L;
			}
		}
	}
}
